package com.yanqu.road.dao.impl.curio;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.curio.CurioItemMaxScoreConditionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CurioItemMaxScoreConditionDataDaoImpl extends TempDataObjectDao<CurioItemMaxScoreConditionData> {

    public static String getCurioItemMaxScoreConditionDataSql = "select * from `t_u_curio_item_max_score`";
    public Map<Integer, CurioItemMaxScoreConditionData> getCurioItemMaxScoreConditionData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CurioItemMaxScoreConditionData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCurioItemMaxScoreConditionDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CurioItemMaxScoreConditionData data = parse(rs);
                map.put(data.getConditionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCurioItemMaxScoreConditionDataSql = "update `t_u_curio_item_max_score` set itemId=?,score=?,userId=?,lastUpdateTime=? where conditionId=?";
    @Override
    protected boolean updateData(CurioItemMaxScoreConditionData curioItemMaxScoreConditionData) {
        curioItemMaxScoreConditionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, curioItemMaxScoreConditionData.getItemId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, curioItemMaxScoreConditionData.getConditionId()));
        if (getDb().executeNoneQuery(updateCurioItemMaxScoreConditionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCurioItemMaxScoreConditionDataSql = "insert into `t_u_curio_item_max_score`(conditionId,itemId,score,userId,lastUpdateTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CurioItemMaxScoreConditionData curioItemMaxScoreConditionData) {
        curioItemMaxScoreConditionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, curioItemMaxScoreConditionData.getConditionId()));
        para.put(i++, new DbParameter(Types.INTEGER, curioItemMaxScoreConditionData.getItemId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, curioItemMaxScoreConditionData.getLastUpdateTime()));
        if (getDb().executeNoneQuery(insertCurioItemMaxScoreConditionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CurioItemMaxScoreConditionData parseData(ResultSet rs) throws SQLException {
        CurioItemMaxScoreConditionData curioItemMaxScoreConditionData = new CurioItemMaxScoreConditionData();
        curioItemMaxScoreConditionData.setConditionId(rs.getInt("conditionId"));
        curioItemMaxScoreConditionData.setItemId(rs.getInt("itemId"));
        curioItemMaxScoreConditionData.setScore(rs.getLong("score"));
        curioItemMaxScoreConditionData.setUserId(rs.getLong("userId"));
        curioItemMaxScoreConditionData.setLastUpdateTime(rs.getLong("lastUpdateTime"));

        return curioItemMaxScoreConditionData;
    }

}
